package admin.controllers;

import java.io.IOException;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admin.forms.DomainForm;
import admin.models.DomainModel;

import entities.Domain;

public class AddBlacklistDomainServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		Domain domain = new Domain();
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("domain", domain);
		
		try {
			request.getRequestDispatcher("/admin/pages/form_domain.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		DomainModel domainData = DomainModel.getInstance();
		
		//Check value valid form
		TreeMap forms = (TreeMap) DomainForm.checkValidDomain(request); 
		Domain domain = (Domain) forms.get("domain");
		TreeMap check = (TreeMap) forms.get("checkValid");
		
		if (check.size() == 0) {
			domain.setStatus(-1);
			domainData.insert(domain);	
			session.setAttribute("success", "Domain " + domain.getDomain() + " has been into blacklist.");
			domain = new Domain();
		}
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("domain", domain);
		try {
			request.getRequestDispatcher("/admin/pages/form_domain.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
